package com.bidevalution.controller.api;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bidevalution.business.context.StepType;
import com.bidevalution.business.context.SysIdentityCode;
import com.bidevalution.business.service.BeLayoutConfigService;
import com.bidevalution.business.service.BeMethodColumnService;
import com.bidevalution.business.service.BeMethodStepService;
import com.bidevalution.business.service.BeSysIdentityService;
import com.bidevalution.controller.dto.BeMethodStepDTO;
import com.bidevalution.controller.vo.BeMethodStepVO;
import com.bidevalution.dao.entity.BeLayoutConfig;
import com.bidevalution.dao.entity.BeMethodColumn;
import com.bidevalution.dao.entity.BeMethodDefine;
import com.bidevalution.dao.entity.BeMethodStep;
import com.bidevalution.dao.mapper.BeLayoutConfigMapper;
import com.bidevalution.support.dto.TreeNodeDTO;
import com.google.common.collect.Maps;
import com.qtp.core.model.Response;
import com.qtp.core.util.IdsUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/beMethodStep")
@Api(tags = "评标环节管理")
public class BeMethodStepController extends BaseController {
	@Autowired
	BeMethodStepService beMethodStepService;
	@Autowired
	private BeLayoutConfigService beLayoutConfigService;
	@Autowired
	private BeSysIdentityService beSysIdentityService;
	@Autowired
	private BeMethodColumnService beMethodColumnService;

	@ApiOperation(value = "查询子环节数据", notes = "根据父节点查询子节环节数据", response = BeMethodStep.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "defineId", value = "方法id", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "parentId", value = "父节点", required = true, dataType = "String", paramType = "query")})
	@GetMapping("/getChildList")
	public Response<List<BeMethodStep>> selectChildList(@RequestParam("defineId") String defineId,@RequestParam("parentId") String parentId) {
		return Response.ok(beMethodStepService.selectForListByParentId(defineId,parentId));
	}
	@ApiOperation(value = "查询子环节数据", notes = "根据父节点查询子节环节数据", response = TreeNodeDTO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "defineId", value = "方法id", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "parentId", value = "父节点", required = true, dataType = "String", paramType = "query")})
	@GetMapping("/getTree")
	public Response<List<TreeNodeDTO>> selectTree(@RequestParam("defineId") String defineId,@RequestParam("parentId") String parentId) {
		return Response.ok(beMethodStepService.selectTree(defineId,parentId));
	}
	@ApiOperation(value = "获取环节类型", notes = "获取环节类型")
	@GetMapping("/getStepTypeList")
	public Response<Map<String,Object>> getStepTypeList() {
		Map<String,Object> dataMap = Maps.newHashMap();
		for(StepType stepType:StepType.values()) {
			dataMap.put(stepType.getCode(), stepType.getText());
		}
		return Response.ok(dataMap);
	}
	@PostMapping("/addBeMethodStep")
	@ApiOperation(value = "新增评标方法环节", notes = "新增评标方法环节")
	public Response<String> add(BeMethodStepDTO beMethodStepDTO){
		BeMethodStep beMethodStep = new BeMethodStep();
		beMethodStep.setSeqId(IdsUtil.uuid());
		beMethodStep.setCreateTime(new Date());
		beMethodStep.setDefineId(beMethodStepDTO.getDefineId());
		beMethodStep.setRelatedColumnId(beMethodStepDTO.getRelatedColumnId());
		beMethodStep.setStepType(beMethodStepDTO.getStepType());
		beMethodStep.setLayoutConfigId(beMethodStepDTO.getLayoutConfigId());
		beMethodStep.setFormulaConfig(beMethodStepDTO.getFormulaConfig());
		beMethodStep.setMemo(beMethodStepDTO.getMemo());
		List<BeMethodStep> stepList = beMethodStepService.selectListByMethodId(beMethodStepDTO.getDefineId());
		//设置排序
		if(stepList.size() == 0) {
			beMethodStep.setOrderNum(1);
		}else {
			beMethodStep.setOrderNum(stepList.get(stepList.size() - 1).getOrderNum()+1);
		}
		beMethodStep.setParentId(beMethodStepDTO.getParentId());
		beMethodStep.setStepCode(beSysIdentityService.getValue(SysIdentityCode.MethodStepCode));
		beMethodStep.setStepName(beMethodStepDTO.getStepName());
		beMethodStep.setLoadFunction(beMethodStepDTO.getLoadFunction());
		beMethodStep.setUpdateTime(new Date());
		beMethodStepService.insert(beMethodStep);
		return Response.ok();
	}
	@PostMapping("/updateBeMethodStep")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "seqId", value = "主键", required = true, dataType = "String", paramType = "query")})
	@ApiOperation(value = "修改评标方法环节", notes = "修改评标方法环节")
	public Response<String> update(@RequestParam(value = "seqId",required = true)String seqId,BeMethodStepDTO beMethodStepDTO){
		BeMethodStep beMethodStep = beMethodStepService.selectById(seqId);
		if(beMethodStep != null) {
			beMethodStep.setMemo(beMethodStepDTO.getMemo());
			beMethodStep.setStepCode(beMethodStepDTO.getStepCode());
			beMethodStep.setStepName(beMethodStepDTO.getStepName());
			beMethodStep.setLoadFunction(beMethodStepDTO.getLoadFunction());
			beMethodStep.setRelatedColumnId(beMethodStepDTO.getRelatedColumnId());
			beMethodStep.setStepType(beMethodStepDTO.getStepType());
			beMethodStep.setFormulaConfig(beMethodStepDTO.getFormulaConfig());
			beMethodStep.setUpdateTime(new Date());
			beMethodStepService.update(beMethodStep);
			return Response.ok();
		}else {
			return Response.fail("记录不存在");
		}
		
	}
	@ApiOperation(value = "查询评标环节信息", notes = "根据主键值查询评标环节信息", response = BeMethodStepVO.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "seqId", value = "主键", required = true, dataType = "String", paramType = "query") })
	@GetMapping("/getMethodStep")
	public Response<BeMethodStepVO> selectById(@RequestParam("seqId") String seqId) {
		BeMethodStep beMethodStep = beMethodStepService.selectById(seqId);
		BeMethodStepVO beMethodStepVO = new BeMethodStepVO();
		beMethodStepVO.setDefineId(beMethodStep.getDefineId());
		beMethodStepVO.setLayoutConfigId(beMethodStep.getLayoutConfigId());
		beMethodStepVO.setLoadFunction(beMethodStep.getLoadFunction());
		beMethodStepVO.setMemo(beMethodStep.getMemo());
		beMethodStepVO.setOrderNum(beMethodStep.getOrderNum());
		beMethodStepVO.setParentId(beMethodStep.getParentId());
		beMethodStepVO.setFormulaConfig(beMethodStep.getFormulaConfig());
		BeMethodStep parentBeMethodStep = beMethodStepService.selectById(beMethodStep.getParentId());
		if(parentBeMethodStep != null) {
			beMethodStepVO.setParentName(parentBeMethodStep.getStepName());
		}
		beMethodStepVO.setRelatedColumnId(beMethodStep.getRelatedColumnId());
		if(!StringUtils.isEmpty(beMethodStep.getRelatedColumnId())) {
			BeMethodColumn beMethodColumn = beMethodColumnService.selectByPrimaryKey(beMethodStep.getRelatedColumnId());
			if(beMethodColumn != null) {
				beMethodStepVO.setRelatedColumnName(beMethodColumn.getColumnText());
			}
		}
		beMethodStepVO.setSeqId(seqId);
		beMethodStepVO.setStepType(beMethodStep.getStepType());
		beMethodStepVO.setStepCode(beMethodStep.getStepCode());
		beMethodStepVO.setStepName(beMethodStep.getStepName());
		BeLayoutConfig beLayoutConfig = beLayoutConfigService.selectByPrimaryKey(beMethodStep.getLayoutConfigId());
		beMethodStepVO.setTemplateUrl(beLayoutConfig==null?"":beLayoutConfig.getTemplateUrl());
		if(beLayoutConfig != null && !StringUtils.isEmpty(beLayoutConfig.getLayoutCode())) {
			beMethodStepVO.setLayoutCode(Arrays.asList(beLayoutConfig.getLayoutCode().split(",")));
		}
		return Response.ok(beMethodStepVO);
	}
	@ApiOperation(value = "删除评标方法", notes = "根据主键删除评标方法")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "seqId", value = "主键", required = true, dataType = "String", paramType = "query")})
	@PostMapping("/delMethodStep")
	public Response<String> del(@RequestParam("seqId") String seqId) {
		beMethodStepService.deleteById(seqId);
		return Response.ok();
	}
}
